from instance import instance
from numpy import inner

true = 1
false = -1

def read_plain(file):
    
    """
    Reads a plain data set file
    
    Each line represents a sample: first number is a label, other are features
    @return: a list of instances 
    """
    
    f = open(file)
    line = f.readline()
    
    instances = []
    
    while (line):
        line = line.split(',')
        inst = []
        for i in line[1:]: inst.append(float(i))
        #inst = inst
        instances.append(instance(list = inst, label = sign(float(line[0]))))
        line = f.readline()
        
    return instances        

def sign(i):
    if i > 0: return true
    else: return false
    
dot = inner

def kernel(d):
    return lambda x1, x2: (1 + dot(x1,x2))**d